Completed
Pull Request — dev (#319)
by Tristan
07:50
created

$(document).ready   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 24
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
// =============================================================================
2
3
    // Utilities JavaScript (jQuery)
4
5
// =============================================================================
6
7
(function($) {
8
9
    // Add isValid()
10
11
        $.fn.isValid = function(){
12
            return this[0].checkValidity()
13
        }
14
15
    $(document).ready(function() {
16
17
        // Accordion Handlers ==================================================
18
19
            function accordionTrigger(trigger) {
20
                if ($(trigger).parent(".accordion").hasClass("active")) {
21
                    $(trigger).attr("aria-expanded", "false");
22
                    $(trigger).parent(".accordion").removeClass("active");
23
                    $(trigger).parent(".accordion").find(".accordion-content").attr("aria-hidden", "true");
24
                }
25
                else {
26
                    $(trigger).attr("aria-expanded", "true");
27
                    $(trigger).parent(".accordion").addClass("active");
28
                    $(trigger).parent(".accordion").find(".accordion-content").attr("aria-hidden", "false");
29
                }
30
            }
31
32
            $(document).on("click", ".accordion-trigger", function(e){
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
33
34
                accordionTrigger(this);
35
36
            });
37
38
            $(document).on("keyup", ".accordion-trigger", function(e){
39
40
                if(e.which == 13) {
41
                    accordionTrigger(this);
42
                }
43
44
            });
45
46
        // Modal Handlers ======================================================
47
48
            function openModal(trigger) {
49
50
                var modalID = $(trigger).attr("data-modal-id");
51
                var modal = $(".modal[data-modal-id="+modalID+"]");
52
                var modalObject = $(trigger).parents(".modal-target-object");
53
                $(".modal-overlay").addClass("active");
54
                modal.addClass("active");
55
                $("body").css("overflow", "hidden");
56
57
                // Tab Items
58
59
                var focusableItems = modal.find(":focusable");
60
61
                var firstInput = focusableItems.first();
62
                var lastInput = focusableItems.last();
63
64
                if (modal.find("form").length == 0) {
0 ignored issues
show
Best Practice introduced by
Comparing modal.find("form").length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
65
                    lastInput.focus();
66
                }
67
                else {
68
                    firstInput.focus();
69
                }
70
71
                modalTabHandler(firstInput, lastInput);
72
                modalDeleteTrigger(trigger, modal, modalObject);
73
                escapeModalHandler();
74
75
            }
76
77
            $(document).on("click", ".modal-trigger", function(e){
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
78
79
                openModal(this);
80
81
            });
82
83
            $(document).on("keyup", ".modal-trigger", function(e){
84
85
                if(e.which == 13) {
86
                    openModal(this);
87
                }
88
89
            });
90
91
92
            function closeModal(trigger) {
0 ignored issues
show
Unused Code introduced by
The parameter trigger is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
93
94
                $(".modal-overlay").removeClass("active");
95
                $(".modal").removeClass("active");
96
                $("body").css("overflow", "visible");
97
98
            }
99
100
            $(document).on("click", ".modal-cancel-trigger", function(e){
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
101
102
                closeModal(this);
103
104
            });
105
106
            $(document).on("keyup", ".modal-cancel-trigger", function(e){
107
108
                if(e.which == 13) {
109
                    closeModal(this);
110
                }
111
112
            });
113
114
            // Delete Trigger ==================================================
115
116
                function modalDeleteTrigger(trigger, modal, object) {
117
118
                    $(document).on("click", ".modal-delete-trigger", function(e){
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
119
120
                        closeModal(trigger);
121
122
                        $(object).remove();
123
124
                    });
125
126
                }
127
128
            // Tab Handler =====================================================
129
130
                function modalTabHandler(first, last) {
131
132
                    $(document).on("keydown", function(e){
133
134
                        var keyCode = e.keyCode || e.which;
135
136
                        if (keyCode == 9 && !e.shiftKey) {
137
138
                            if ($(last).is(":focus")) {
139
                                e.preventDefault();
140
                                $(first).focus();
141
                            }
142
143
                        }
144
                        else if (keyCode == 9 && e.shiftKey) {
145
146
                            if($(first).is(":focus")) {
147
                                e.preventDefault();
148
                                $(last).focus();
149
                            }
150
151
                        }
152
153
                    });
154
155
                }
156
157
            // Escape Handler ==================================================
158
159
                function escapeModalHandler() {
160
161
                    $(document).on("keyup", function(e){
162
163
                        if((e.key==='Escape'||e.key==='Esc'||e.keyCode===27)){
164
165
                            $(".modal-overlay").removeClass("active");
166
                            $(".modal").removeClass("active");
167
                            $("body").css("overflow", "visible");
168
169
                            // FF and compatible
170
                            if (e.stopPropagation) {
171
                                e.stopPropagation();
172
                                e.preventDefault();
173
                            }
174
175
                        }
176
177
                    });
178
179
                }
180
181
        // Form Handlers =======================================================
182
183
            // Required Fields
184
185
                function requiredFields() {
186
                    $("input:required, textarea:required").each(function(e) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
187
                        $(this).parent().addClass("required");
188
                        $(this).parent().find("label").append("<span class='form__required'><i class='fa fa-asterisk' aria-label='Asterisk'></i></span>");
189
                    });
190
                }
191
192
                requiredFields();
193
194
            // Label Handers ===================================================
195
196
                function labelHandlers() {
197
198
                    $("[class*='form__input-wrapper'] input, [class*='form__input-wrapper'] textarea").focusin(function(e) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
199
                        $(this).parent().addClass("active");
200
                    });
201
202
                    $("[class*='form__input-wrapper'] input, [class*='form__input-wrapper'] textarea").focusout(function(e) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
203
204
                        // Check for existing value.
205
206
                            if ($(this).val() == "") {
207
                                $(this).parent().removeClass("active");
208
                            }
209
210
                        // Check Validity
211
212
                            if ($(this).isValid() == true) {
0 ignored issues
show
Best Practice introduced by
Comparing $(this).isValid() to true using the == operator is not safe. Consider using === instead.
Loading history...
213
214
                                if ($(this).val() == "" || $(this).attr("type") == "password") {
215
                                    $(this).parent().removeClass("valid");
216
                                    $(this).parent().removeClass("invalid");
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
217
                                }
218
                                else {
219
                                    $(this).parent().addClass("valid");
220
                                    $(this).parent().removeClass("invalid");
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
221
                                }
222
223
                            }
224
                            else {
225
226
                                if ($(this).attr("type") == "password") {
227
                                    return false;
228
                                }
229
                                else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
230
                                    $(this).parent().addClass("invalid");
231
                                    $(this).parent().removeClass("valid");
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
232
                                }
233
234
                            }
235
236
                    });
237
238
                }
239
240
                labelHandlers();
241
242
        // Individualizing repeater name and id attributes======================
243
244
                //Individualize template attributes
245
                function appendToAttributes(parent, attribute, suffix, conditions = null) {
0 ignored issues
show
introduced by
The function appendToAttributes does not seem to be used and can be removed.
Loading history...
246
                    var selector = "*[" + attribute + "]";
247
248
                    //If conditions is set, only modify attributes that also
249
                    //satisfy that selector
250
                    if (conditions) {
251
                        selector = conditions + selector;
252
                    }
253
254
                    parent.find(selector).each(function() {
255
                        $(this).attr(attribute, $(this).attr(attribute) + suffix);
256
                    });
257
                }
258
259
                //Individualize template attributes
260
                function replaceInAttributes(parent, attribute, oldString, newString, conditions = null) {
261
                    var selector = "*[" + attribute + "]";
262
263
                    //If conditions is set, only modify attributes that also
264
                    //satisfy that selector
265
                    if (conditions) {
266
                        selector = conditions + selector;
267
                    }
268
269
                    parent.find(selector).each(function() {
270
                        //replaces only the first instance of a match in a string
271
                        $(this).attr(attribute, $(this).attr(attribute).replace(oldString, newString));
272
                    });
273
                }
274
275
                //Return the next unused idAttr value
276
                function getNextItemId(parent, idAttr = "data-item-id") {
277
                    var maxId = 0;
278
                    parent.find("*[" + idAttr + "]").each(function() {
279
                        var id = parseInt( $(this).attr(idAttr) );
280
                        if (id > maxId) {
281
                            maxId = id;
282
                        }
283
                    });
284
                    return maxId + 1;
285
                }
286
287
                //The all in one function to set proper ids and form names
288
                function individualizeFormIdsAndNames(template, wrapper) {
289
                    // Get New ID
290
                    var newId = getNextItemId(wrapper);
291
292
                    //Set date-item-id, used to track which newId's are taken
293
                    template.attr('data-item-id', newId);
294
295
                    //Differentiate real forms from templates
296
297
                    // filter, if we only want to affect certain results
298
                    var filter = '';
299
300
                    replaceInAttributes(template, 'id', ':template', 'new', filter);
301
                    replaceInAttributes(template, 'for', ':template', 'new', filter);
302
                    replaceInAttributes(template, 'name', ':template', 'new', filter);
303
                    replaceInAttributes(template, 'submit', ':template', 'new', filter);
304
                    replaceInAttributes(template, 'value', ':template', 'new', filter+'[name=submit]');
305
306
                    replaceInAttributes(template, 'id', ':id', newId, filter);
307
                    replaceInAttributes(template, 'for', ':id', newId, filter);
308
                    replaceInAttributes(template, 'name', ':id', newId, filter);
309
                    replaceInAttributes(template, 'submit', ':id', newId, filter);
310
                    replaceInAttributes(template, 'value', ':id', newId, filter+'[name=submit]');
311
                }
312
313
        // Profile List Handlers ===============================================
314
315
            // Add Profile Element
316
                function addProfileElement(trigger) {
317
318
                    // Get Parent
319
                        var parent = $(trigger).parents(".profile-list");
320
321
                    // Get List Wrapper
322
                        var wrapper = parent.find(".profile-element-list");
323
324
                    // Set Null to Hidden
325
                        parent.find(".profile-null").removeClass("active");
326
327
                    // Get Template
328
                        var template = parent.find(".profile-element.template").clone();
329
330
                    // Remove Template Class
331
                        template.removeClass("template");
332
333
                    //Set ids and form names to be unique
334
                    individualizeFormIdsAndNames(template, wrapper);
335
336
                    // Prepend Clone to the Wrapper
337
                    wrapper.prepend(template);
338
339
                    // Reactivate Required Fields
340
                        requiredFields();
341
342
                    // Reactivate Labels
343
                        labelHandlers();
344
345
                    // Reactivate Nested Relatives
346
                        loadProfileRelatives();
347
348
                }
349
350
                // Click Trigger
351
                    $(".profile-list__add-element-trigger").on("click", function(e) {
352
353
                        // Prevent Default Functions
354
                            e.preventDefault();
355
356
                        // Add Profile Elements
357
                            addProfileElement(this);
358
359
                    });
360
361
                // Enter Key Trigger
362
                    $(".profile-list__add-element-trigger").on("keyup", function(e) {
363
364
                        if(e.which == 13) {
365
366
                            // Prevent Default Functions
367
                                e.preventDefault();
368
369
                            // Add Profile Elements
370
                                addProfileElement(this);
371
372
                        }
373
374
                    });
375
376
            // Remove Profile Element
377
378
            // Add Profile Relative
379
                function addProfileRelative(trigger) {
380
381
                    // Get Parent
382
                        var parent = $(trigger).parents(".profile-relative-list");
383
384
                    // Get List Wrapper
385
                        var wrapper = parent.find(".profile-relative-list__wrapper");
386
387
                    // Set Null to Hidden
388
                        // parent.find(".profile-null").removeClass("active");
389
390
                    // Get Template
391
                        var template = parent.find(".profile-relative.template").clone();
392
393
                    // Remove Template Class
394
                        template.removeClass("template");
395
396
                    //Set ids and form names to be unique
397
                    individualizeFormIdsAndNames(template, wrapper);
398
399
                    // Append Clone to the Wrapper
400
                    wrapper.append(template);
401
402
                    // Reactivate Required Fields
403
                        requiredFields();
404
405
                    // Reactivate Labels
406
                        labelHandlers();
407
408
                    // Reactivate Nested Relatives
409
                        loadProfileRelativeDeletion();
410
411
                }
412
413
                // Load Function
414
                    function loadProfileRelatives() {
415
416
                        // Click Trigger
417
                            $(".profile-relative__add-trigger").off("click");
418
419
                            $(".profile-relative__add-trigger").on("click", function(e) {
420
421
                                // Prevent Default Functions
422
                                    e.preventDefault();
423
424
                                // Add Profile Relative
425
                                    addProfileRelative(this);
426
427
                            });
428
429
                        // Enter Key Trigger
430
                            $(".profile-relative__add-trigger").off("keyup");
431
432
                            $(".profile-relative__add-trigger").on("keyup", function(e) {
433
434
                                if(e.which == 13) {
435
436
                                    // Prevent Default Functions
437
                                        e.preventDefault();
438
439
                                    // Add Profile Relative
440
                                        addProfileRelative(this);
441
442
                                }
443
444
                            });
445
446
                    }
447
448
                    loadProfileRelatives();
449
450
            // Remove Profile Relative
451
                function deleteProfileRelative(trigger) {
452
453
                    $(trigger).parents(".profile-relative").remove();
454
455
                }
456
457
                // Load Function
458
                    function loadProfileRelativeDeletion() {
459
460
                        // Click Trigger
461
                            $(".profile-relative__remove-trigger").on("click", function(e) {
462
463
                                // Prevent Default Functions
464
                                    e.preventDefault();
465
466
                                // Delete Profile Relative
467
                                    deleteProfileRelative(this);
468
469
                            });
470
471
                        // Enter Key Trigger
472
                            $(".profile-relative__remove-trigger").on("keyup", function(e) {
473
474
                                if(e.which == 13) {
475
476
                                    // Prevent Default Functions
477
                                        e.preventDefault();
478
479
                                    // Delete Profile Relative
480
                                        deleteProfileRelative(this);
481
482
                                }
483
484
                            });
485
486
                    }
487
488
                    loadProfileRelativeDeletion();
489
490
        // Experience Handlers =================================================
491
492
            // Degrees
493
494
                function addDegree(trigger) {
0 ignored issues
show
Unused Code introduced by
The parameter trigger is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
495
496
                    // Get Wrapper
497
                    var wrapper = $(".application-post__experience-wrapper");
498
499
                    // Get Template
500
                    var template = $(".application-post__accordion--degree.template").clone();
501
502
                    // Remove Template Class
503
                    template.removeClass("template");
504
505
                    //Set ids and form names to be unique
506
                    individualizeFormIdsAndNames(template, wrapper);
507
508
                    // Append Clone to the Wrapper
509
                    wrapper.append(template);
510
511
                    requiredFields();
512
                    labelHandlers();
513
514
                }
515
516
                $("#addDegreeButton").on("click", function(e) {
517
518
                    e.preventDefault();
519
520
                    addDegree(this);
521
522
                });
523
524
                $("#addDegreeButton").on("keyup", function(e) {
525
526
                    if(e.which == 13) {
527
                        e.preventDefault();
528
                        addDegree(this);
529
                    }
530
531
                });
532
533
            // Courses
534
535
                function addCourse(trigger) {
0 ignored issues
show
Unused Code introduced by
The parameter trigger is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
536
537
                    // Get Wrapper
538
                    var wrapper = $(".application-post__experience-wrapper");
539
540
                    // Get Template
541
                    var template = $(".application-post__accordion--course.template").clone();
542
543
                    // Remove Template Class
544
                    template.removeClass("template");
545
546
                    //Set ids and form names to be unique
547
                    individualizeFormIdsAndNames(template, wrapper);
548
549
                    // Append Clone to the Wrapper
550
                    wrapper.append(template);
551
552
                    requiredFields();
553
                    labelHandlers();
554
555
                }
556
557
                $("#addCourseButton").on("click", function(e) {
558
559
                    e.preventDefault();
560
561
                    addCourse(this);
562
563
                });
564
565
                $("#addCourseButton").on("keyup", function(e) {
566
567
                    if(e.which == 13) {
568
                        e.preventDefault();
569
                        addCourse(this);
570
                    }
571
572
                });
573
574
            // Work
575
576
                function addWork(trigger) {
0 ignored issues
show
Unused Code introduced by
The parameter trigger is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
577
578
                    // Get Wrapper
579
                    var wrapper = $(".application-post__experience-wrapper");
580
581
                    // Get Template
582
                    var template = $(".application-post__accordion--work.template").clone();
583
584
                    // Remove Template Class
585
                    template.removeClass("template");
586
587
                    //Set ids and form names to be unique
588
                    individualizeFormIdsAndNames(template, wrapper);
589
590
                    // Append Clone to the Wrapper
591
                    wrapper.append(template);
592
593
                    requiredFields();
594
                    labelHandlers();
595
596
                }
597
598
                $("#addWorkButton").on("click", function(e) {
599
600
                    e.preventDefault();
601
602
                    addWork(this);
603
604
                });
605
606
                $("#addWorkButton").on("keyup", function(e) {
607
608
                    if(e.which == 13) {
609
                        e.preventDefault();
610
                        addWork(this);
611
                    }
612
613
                });
614
615
        // Create Job Handlers =================================================
616
617
            // Tasks
618
619
                function addTask(trigger) {
0 ignored issues
show
Unused Code introduced by
The parameter trigger is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
620
621
                    // Get Wrapper
622
                    var wrapper = $(".manager-jobs__create-task-wrapper");
623
624
                    // Get Template
625
                    var template = $(".manager-jobs__create-task.template").clone();
626
627
                    console.log(wrapper.find(".manager-jobs__create-task"));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
628
629
                    // Get New ID
630
                    if (wrapper.find(".manager-jobs__create-task").length == 0) {
0 ignored issues
show
Best Practice introduced by
Comparing wrapper.find(".manager-jobs__create-task").length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
631
                        var newID = parseInt(template.attr("data-task-id")) + 1;
632
                    }
633
                    else {
634
                        var newID = parseInt(wrapper.find("[class*='manager-jobs__create-task']").last().attr("data-task-id")) + 1;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable newID already seems to be declared on line 631. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
635
                    }
636
637
                    // Remove Template Class
638
                    template.removeClass("template");
639
640
                    //TODO: replace with call to individualizeFormIdsAndNames(template, wrapper);
641
                    //TODO: This requires changes to JobController@create, because the id would change places
642
643
                    // Assign the New ID
644
                    template.attr("data-task-id", newID);
645
646
                    // Add newID as suffix to all "id" and "for" attributes
647
                    template.find("*[id]").each(function() { $(this).attr("id", this.id + newID)});
648
                    template.find("*[for]").each(function() { $(this).attr("for",  $(this).attr("for") + newID)});
649
650
                    // Replace :id with newID in all form names
651
                    template.find("*[name]").each(function() { $(this).attr('name', $(this).attr("name").replace(":id", newID))});
652
653
                    // Task (English)
654
                    //template.find("[data-form-id*='task-english']").find("label").attr("for", "taskEN" + newID);
655
                    //template.find("[data-form-id*='task-english']").find("input").attr("id", "taskEN" + newID);
656
657
                    // Task (French)
658
                    //template.find("[data-form-id*='task-french']").find("label").attr("for", "taskFR" + newID);
659
                    //template.find("[data-form-id*='task-french']").find("input").attr("id", "taskFR" + newID);
660
661
                    // Append Clone to the Wrapper
662
                    wrapper.append(template);
663
664
                    requiredFields();
665
                    labelHandlers();
666
                    deleteTaskTrigger();
667
668
                }
669
670
                $("#addTaskButton").on("click", function(e) {
671
672
                    e.preventDefault();
673
674
                    addTask(this);
675
676
                });
677
678
                $("#addTaskButton").on("keyup", function(e) {
679
680
                    if(e.which == 13) {
681
                        e.preventDefault();
682
                        addTask(this);
683
                    }
684
685
                });
686
687
                // Task Deletion
688
689
                function deleteTask(trigger) {
690
691
                    $(trigger).parents(".manager-jobs__create-task").remove();
692
693
                }
694
695
                function deleteTaskTrigger() {
696
697
                    $(".manager-jobs__delete-task-button").on("click", function(e) {
698
699
                        e.preventDefault();
700
701
                        deleteTask(this);
702
703
                    });
704
705
                    $(".manager-jobs__delete-task-button").on("keyup", function(e) {
706
707
                        if(e.which == 13) {
708
                            e.preventDefault();
709
                            deleteTask(this);
710
                        }
711
712
                    });
713
714
                }
715
716
                deleteTaskTrigger();
717
718
            // Skills
719
720
                function addSkill(trigger) {
721
722
                    // Get Parent
723
                    var parent = $(trigger).parents(".manager-jobs__skill-wrapper");
724
725
                    // Get Wrapper
726
                    var wrapper = parent.find(".manager-jobs__create-skill-wrapper");
727
728
                    // Get Template
729
                    var template = parent.find(".manager-jobs__create-skill.template").clone();
730
731
                    console.log(wrapper.find(".manager-jobs__create-skill"));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
732
733
                    // Remove Template Class
734
                    template.removeClass("template");
735
736
                    //Set ids and form names to be unique
737
                    individualizeFormIdsAndNames(template, wrapper);
738
739
                    // Append Clone to the Wrapper
740
                    wrapper.append(template);
741
742
                    requiredFields();
743
                    labelHandlers();
744
                    deleteSkillTrigger();
745
746
                }
747
748
                $(".manager-jobs__add-skill-button").on("click", function(e) {
749
750
                    e.preventDefault();
751
752
                    addSkill(this);
753
754
                });
755
756
                $(".manager-jobs__add-skill-button").on("keyup", function(e) {
757
758
                    if(e.which == 13) {
759
                        e.preventDefault();
760
                        addSkill(this);
761
                    }
762
763
                });
764
765
                // Skill Deletion
766
767
                function deleteSkill(trigger) {
768
769
                    $(trigger).parents(".manager-jobs__create-skill").remove();
770
771
                }
772
773
                function deleteSkillTrigger() {
774
775
                    $(".manager-jobs__delete-skill-button").on("click", function(e) {
776
777
                        e.preventDefault();
778
779
                        deleteSkill(this);
780
781
                    });
782
783
                    $(".manager-jobs__delete-skill-button").on("keyup", function(e) {
784
785
                        if(e.which == 13) {
786
                            e.preventDefault();
787
                            deleteSkill(this);
788
                        }
789
790
                    });
791
792
                }
793
794
                deleteSkillTrigger();
795
796
            // Questions
797
798
                function addQuestion(trigger) {
0 ignored issues
show
Unused Code introduced by
The parameter trigger is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
799
800
                    // Get Wrapper
801
                    var wrapper = $(".manager-jobs__create-question-wrapper");
802
803
                    // Get Template
804
                    var template = $(".manager-jobs__create-question.template").clone();
805
806
                    console.log(wrapper.find(".manager-jobs__create-question"));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
807
808
                    // Get New ID
809
                    if (wrapper.find(".manager-jobs__create-question").length == 0) {
0 ignored issues
show
Best Practice introduced by
Comparing wrapper.find(".manager-j...reate-question").length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
810
                        var newID = parseInt(template.attr("data-question-id")) + 1;
811
                    }
812
                    else {
813
                        var newID = parseInt(wrapper.find("[class*='manager-jobs__create-question']").last().attr("data-question-id")) + 1;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable newID already seems to be declared on line 810. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
814
                    }
815
816
                    // Remove Template Class
817
                    template.removeClass("template");
818
819
                    //TODO: replace with call to individualizeFormIdsAndNames(template, wrapper);
820
                    //TODO: This requires changes to JobController@create, because the id would change places
821
822
                    // Assign the New ID
823
                    template.attr("data-question-id", newID);
824
825
                    // Add newID as suffix to all "id" and "for" attributes
826
                    template.find("*[id]").each(function() { $(this).attr("id", this.id + newID)});
827
                    template.find("*[for]").each(function() { $(this).attr("for",  $(this).attr("for") + newID)});
828
829
                    // Replace :id with newID in all form names
830
                    template.find("*[name]").each(function() { $(this).attr('name', $(this).attr("name").replace(":id", newID))});
831
832
                    // Edit Form IDs
833
                        //
834
                        // // Queestion (English)
835
                        // template.find("[data-form-id*='question-english']").find("label").attr("for", "questionEN" + newID);
836
                        // template.find("[data-form-id*='question-english']").find("input").attr("id", "questionEN" + newID);
837
                        //
838
                        // // Queestion (French)
839
                        // template.find("[data-form-id*='question-french']").find("label").attr("for", "questionFR" + newID);
840
                        // template.find("[data-form-id*='question-french']").find("input").attr("id", "questionFR" + newID);
841
842
                    // Append Clone to the Wrapper
843
                    wrapper.append(template);
844
845
                    requiredFields();
846
                    labelHandlers();
847
                    deleteQuestionTrigger();
848
849
                }
850
851
                $("#addQuestionButton").on("click", function(e) {
852
853
                    e.preventDefault();
854
855
                    addQuestion(this);
856
857
                });
858
859
                $("#addQuestionButton").on("keyup", function(e) {
860
861
                    if(e.which == 13) {
862
                        e.preventDefault();
863
                        addQuestion(this);
864
                    }
865
866
                });
867
868
                // Question Deletion
869
870
                function deleteQuestion(trigger) {
871
872
                    $(trigger).parents(".manager-jobs__create-question").remove();
873
874
                }
875
876
                function deleteQuestionTrigger() {
877
878
                    $(".manager-jobs__delete-question-button").on("click", function(e) {
879
880
                        e.preventDefault();
881
882
                        deleteQuestion(this);
883
884
                    });
885
886
                    $(".manager-jobs__delete-question-button").on("keyup", function(e) {
887
888
                        if(e.which == 13) {
889
                            e.preventDefault();
890
                            deleteQuestion(this);
891
                        }
892
893
                    });
894
895
                }
896
897
                deleteQuestionTrigger();
898
899
    });
900
901
})(jQuery);
902